{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-22T13:40:23.536841Z",
     "start_time": "2025-04-22T13:40:20.270815Z"
    }
   },
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "# 加载鸢尾花数据集\n",
    "iris = load_iris()\n",
    "X_iris = iris.data\n",
    "y_iris = iris.target\n",
    "\n",
    "# 按照测试集与训练集的比例为2:8划分\n",
    "X_train_iris, X_test_iris, y_train_iris, y_test_iris = train_test_split(X_iris, y_iris, test_size=0.2, random_state=42)\n",
    "\n",
    "# 输出数据集划分结果\n",
    "print(f\"训练集大小: {X_train_iris.shape[0]}\")\n",
    "print(f\"测试集大小: {X_test_iris.shape[0]}\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集大小: 120\n",
      "测试集大小: 30\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-22T13:42:13.862666Z",
     "start_time": "2025-04-22T13:42:13.561867Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n",
    "from sklearn.linear_model import Perceptron\n",
    "\n",
    "# 使用感知机模型对鸢尾花数据集进行训练\n",
    "perceptron_iris = Perceptron(max_iter=1000, tol=1e-3)\n",
    "perceptron_iris.fit(X_train_iris, y_train_iris)\n",
    "\n",
    "# 测试模型\n",
    "y_pred_iris = perceptron_iris.predict(X_test_iris)\n",
    "\n",
    "# 计算评估指标\n",
    "accuracy_iris = accuracy_score(y_test_iris, y_pred_iris)\n",
    "precision_iris = precision_score(y_test_iris, y_pred_iris, average='weighted')\n",
    "recall_iris = recall_score(y_test_iris, y_pred_iris, average='weighted')\n",
    "f1_iris = f1_score(y_test_iris, y_pred_iris, average='weighted')\n",
    "conf_matrix_iris = confusion_matrix(y_test_iris, y_pred_iris)\n",
    "\n",
    "# 输出结果\n",
    "print(\"感知机模型（鸢尾花数据集）：\")\n",
    "print(f\"准确率: {accuracy_iris}\")\n",
    "print(f\"精确率: {precision_iris}\")\n",
    "print(f\"召回率: {recall_iris}\")\n",
    "print(f\"F1分数: {f1_iris}\")\n",
    "print(f\"混淆矩阵:\\n{conf_matrix_iris}\")"
   ],
   "id": "4a2b3437dfc77b90",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感知机模型（鸢尾花数据集）：\n",
      "准确率: 0.8\n",
      "精确率: 0.875\n",
      "召回率: 0.8\n",
      "F1分数: 0.7730769230769231\n",
      "混淆矩阵:\n",
      "[[10  0  0]\n",
      " [ 6  3  0]\n",
      " [ 0  0 11]]\n"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}